package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.service.INewsPageService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 收藏的资源控制器
 */
@RestController
@RequestMapping("collect")
public class CollectController {
    @Autowired
    private ICollectService collectService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private INewsPageService newsPageService;

    /**
     * 新增/删除
     *
     * @param collect
     * @return
     */
    @GetMapping
    public JSONResult collect(Collect collect) {
        JSONResult result = new JSONResult();

        Collect con = collectService.selectOne(collect);
        if (con == null) {
            collectService.saveOne(collect);
        } else {
            collectService.deleteOne(collect);
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 查询是否有该条收藏
     *
     * @param collect
     * @return
     */
    @GetMapping("select")
    public JSONResult select(Collect collect) {
        JSONResult result = new JSONResult();
        Collect con = collectService.selectOne(collect);
        if (con == null) {
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 计数
     *
     * @param collect
     * @return
     */
    @GetMapping("count")
    public Map selectForCount(Collect collect) {
        HashMap<String, Object> map = new HashMap<>();
        int count = collectService.selectForCount(collect);
        map.put("count", count);
        return map;
    }

    /**
     * 查询用户收藏的攻略/游记/日报
     * @param qo
     * @return
     */
    @GetMapping("list/count")
    public PageInfo listForCount(CollectQueryObject qo) {
        if(qo.getType() == 1){
            PageInfo pageInfo = strategyService.selectForCollect(qo);
            return pageInfo;
        } else if(qo.getType() == 2){
            return travelService.selectForCollect(qo);
        } else if(qo.getType() == 3){
            return newsPageService.selectForCollect(qo);
        }
        return null;
    }
}
